/**
 * PlaceHolder v1.0
 * 
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */

function placeholder(){
    var allElements = document.getElementsByTagName("*");
    var color = "rgb(190,190,190)";
    for (var i = 0; i < allElements.length; i++){
        if (allElements[i].tagName.toLowerCase() == "form"){
            allElements[i].onsubmit = (function(){
                var formElements = this.getElementsByTagName("*");
                for (var x = 0; x < formElements.length; x++){
                    if (hexToRgb(formElements[x].style.color.replace(/\s/g,"")) == color)
                        formElements[x].value = "";
                }
            });
        }
        if (allElements[i].getAttribute("placeholder")){
            if (allElements[i].tagName.toLowerCase() == "select"){
                var option = document.createElement("option");
                option.text = allElements[i].getAttribute("placeholder");
                option.value = "";
                try {
                    allElements[i].add(option, allElements[i].options[0]);
                }
                catch(ex){
                    allElements[i].add(option, 0);
                }
                if (allElements[i].options[1].selected)
                    allElements[i].options[0].selected = "selected";
                allElements[i].options[0].disabled = "disabled";
                var optionElements = allElements[i].getElementsByTagName("option");
                optionElements[0].style.color = color;
                for (var x = 1; x < optionElements.length; x++){
                    optionElements[x].style.color = hexToRgb("#333");
                }
                if (allElements[i].value == "")
                    allElements[i].style.color = color;
                allElements[i].onchange = (function(){
                    if (this.options[this.selectedIndex].value == "" && this.options[this.selectedIndex].text == "")
                        this.options[0].selected = "selected";
                    if (this.selectedIndex == 0)
                        this.style.color = color;
                    else
                        this.style.color = "";
                });
                allElements[i].onkeyup = (function(e){
                    if (!e) e = window.event;
                    if (e.keyCode == 33 || e.keyCode == 36 || e.keyCode == 37 || e.keyCode == 38){
                        if (this.options[this.selectedIndex].value == "" && this.options[this.selectedIndex].text == "")
                            this.options[0].selected = "selected";
                    }
                    if (e.keyCode == 34 || e.keyCode == 35 || e.keyCode == 39 || e.keyCode == 40){
                        if (this.options[this.selectedIndex].value == "" && this.options[this.selectedIndex].text == "")
                            this.options[this.selectedIndex+1].selected = "selected";
                    }
                    if (this.selectedIndex == 0)
                        this.style.color = color;
                    else
                        this.style.color = "";
                });
            }
            else{
                if (allElements[i].value == ""){
                    allElements[i].style.color = color;
                    allElements[i].value = allElements[i].getAttribute("placeholder");
                }
                allElements[i].onclick = (function(){
                    if (hexToRgb(this.style.color.replace(/\s/g,"")) == color){
                        SelectionRange(this,0,0);
                    }
                });
                allElements[i].onkeydown = (function(e){
                    if (!e) e = window.event;
                    if (e.keyCode != 13 && e.keyCode != 9 && e.keyCode != 116 &&
                        e.keyCode != 117 && e.keyCode != 122 && e.keyCode != 123 &&
                        !(e.keyCode > 47 && e.keyCode < 91) && !(e.keyCode > 95 && e.keyCode < 113)){
                        if (hexToRgb(this.style.color.replace(/\s/g,"")) == color)
                            e.preventDefault ? e.preventDefault() : e.returnValue = false;
                    }
                    else if (e.keyCode != 9) {
                        if (hexToRgb(this.style.color.replace(/\s/g,"")) == color){
                            this.value = "";
                            this.style.color = "";
                        }
                    }
                });
                if (navigator.userAgent.indexOf("Opera") != -1) {
                    allElements[i].onkeypress = (function(e){
                        if (!e) e = window.event;
                        if (e.keyCode != 13 && e.keyCode != 9 && e.keyCode != 116 &&
                            e.keyCode != 117 && e.keyCode != 122 && e.keyCode != 123 &&
                            !(e.keyCode > 47 && e.keyCode < 91) && !(e.keyCode > 95 && e.keyCode < 113)){
                            if (hexToRgb(this.style.color.replace(/\s/g,"")) == color)
                                e.preventDefault ? e.preventDefault() : e.returnValue = false;
                        }
                    });
                }
                allElements[i].onkeyup = (function(){
                    if (this.value == ""){
                        this.value = this.getAttribute("placeholder");
                        this.style.color = color;
                        SelectionRange(this,0,0);
                    }
                });
                if (allElements[i].getAttribute("type") == "password"){
                    allElements[i].onkeyup = (function(e){
                        if (this.value == this.getAttribute("placeholder")){
                            this.setAttribute("type", "text");
                        }
                        else {
                            this.setAttribute("type", "password");
                        }
                    });
                    allElements[i].setAttribute("type", "text");
                }
            }
        }
    }
}

function SelectionRange(input, start, end){
    if (input.setSelectionRange)
        input.setSelectionRange(start,end);
    else if (input.createTextRange){
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', end);
        range.moveStart('character', start);
        range.select();
    }
}

function hexToRgb(hex) {
    colorPattern = /^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
    if (colorPattern.test(hex) == false)
        return hex;
    
    var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
    hex = hex.replace(shorthandRegex, function(m, r, g, b) {
        return r + r + g + g + b + b;
    });
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return "rgb("+parseInt(result[1], 16)+","+parseInt(result[2], 16)+","+parseInt(result[3], 16)+")";
}